package cn.bonoon.controllers.fmm;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.bonoon.core.plugins.RoleService;
import cn.bonoon.entities.RoleEntity;
import cn.bonoon.kernel.events.DialogEvent;
import cn.bonoon.kernel.menus.MenuCheckable;
import cn.bonoon.kernel.menus.MenuViewBuilder;
import cn.bonoon.kernel.menus.ModuleDefinition;
import cn.bonoon.kernel.support.services.IService;
import cn.bonoon.kernel.util.StringHelper;
import cn.bonoon.kernel.web.ButtonEventType;
import cn.bonoon.kernel.web.annotations.grid.GridStandardDefinition;
import cn.bonoon.kernel.web.controllers.AbstractGridController;
import cn.bonoon.kernel.web.handlers.DialogDetailHandler;
import cn.bonoon.kernel.web.handlers.DialogFormInitializer;
import cn.bonoon.kernel.web.handlers.DialogInsertHandler;
import cn.bonoon.kernel.web.handlers.DialogUpdateHandler;
import cn.bonoon.kernel.web.models.DialogModel;

@Controller
@RequestMapping("s/sys/role")
public class RoleController extends AbstractGridController<RoleEntity, RoleItem> {
	
	private final RoleService service;

	@Autowired
	public RoleController(RoleService service) {
		super(service);
		this.service = service;
	}

	@RequestMapping("!{key}/{module}!privileges.do")
	public String privileges(Model model, 
			@PathVariable("key") String key, 
			@PathVariable("module") String module, 
			@RequestParam("roleid") Long roleid) {
		try{
			MenuViewBuilder builder = moduleManager.menuBuilder(MenuCheckable.CHECKABLE, false, module);
			builder.addRoles(service, roleid).parse();
			model.addAttribute("privileges", builder);
		}catch (Exception e) {
			log(e);
		}
		model.addAttribute("layout", "layout-empty.vm");
		return "sys/account/module-menu-editable";
	}

	private class RoleFormInitializer implements DialogFormInitializer<RoleEntity> {
		@Override
		public Object init(IService<RoleEntity> is, DialogModel model, DialogEvent event, Long id, Object form) {
			String moduleKey = null; // 子系统
			RoleEditor editor = (RoleEditor)form;
			if(null != editor){
				moduleKey = editor.getModuleKey();
			}
			
			StringBuilder modules = new StringBuilder();
			for (ModuleDefinition m : moduleManager.modules()) {// 获取所有子系统
				String mk = m.getId();
				modules.append("<option");
				if (StringHelper.isEmpty(moduleKey) && null == id) {
					moduleKey = mk;
				}else if(mk.equals(moduleKey)){
					modules.append(" selected='selected'");
				}
				modules.append(" value='").append(mk).append("'>").append(m.getName()) .append("</option>");
			}
			model.setHeight(450);
			model.addObject("id", id);
			model.addObject("moduleKey", modules);
			
			StringBuilder html = new StringBuilder("<div id='div-privileges'>");
			MenuViewBuilder builder = moduleManager.menuBuilder(MenuCheckable.CHECKABLE, false, moduleKey);
			builder.addRoles(service, id).parse(html);
			html.append("</div>");
			
			model.addObject("privileges", builder);
			return form;
		}
	}
	
	private class RoleDetailInitializer implements DialogFormInitializer<RoleEntity> {
		@Override
		public Object init(IService<RoleEntity> is, DialogModel model, DialogEvent event, Long id, Object form) {
			RoleDetail detail = (RoleDetail)form;
			MenuViewBuilder builder = moduleManager.menuBuilder(MenuCheckable.READONLY, false, detail.getModuleKey());
			builder.addRoles(service, id).parse();
			
			detail.setPrivileges(builder.toString());
			return form;
		}
	}

	@Override
	@GridStandardDefinition
	protected RoleService initLayoutGrid(LayoutGridRegister register) throws Exception {
		DialogInsertHandler<RoleEntity> dih = new DialogInsertHandler<RoleEntity>(register, service, RoleEditor.class);
		dih.setFormInitializer(new RoleFormInitializer());
		dih.register(register.toolbar()).ordinal(0);

		DialogDetailHandler<RoleEntity> ddh = new DialogDetailHandler<RoleEntity>(register, service, RoleDetail.class);
		ddh.setFormInitializer(new RoleDetailInitializer());
		ddh.register(register.button()).ordinal(0);
		
		DialogUpdateHandler<RoleEntity> duh = new DialogUpdateHandler<RoleEntity>(register, service, RoleEditor.class);
		duh.setFormInitializer(new RoleFormInitializer());
		duh.register(register.button()).ordinal(1);
		
		register.button("分配账号", "accounts.do", ButtonEventType.DIALOG).ordinal(20);
		register.button("分配用户组", "groups.do", ButtonEventType.DIALOG).ordinal(21);
		
		register.button("角色策略", "resetpwd.do", ButtonEventType.DIALOG).ordinal(30);
		
		return service;
	}

	@Override
	protected Class<RoleItem> itemClass() {
		return RoleItem.class;
	}
}
